<template>
  <div>
    <el-card>
      <!-- 搜索 -->
      <el-form :inline="true" :model="orderSerach" class="demo-form-inline">
        <el-form-item label="订单号:">
          <el-input
            v-model="orderSerach.orderSn"
            placeholder="请输入订单号"
            clearable
            @change="getOrder"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input
            v-model="orderSerach.contactPhone"
            placeholder="请输入手机号"
            clearable
            @change="getOrder"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getOrder">查询</el-button>
          <el-button type="success" @click="showOrder">新增订单信息</el-button>
        </el-form-item>
      </el-form>
      <!-- 表格 -->

      <el-table :data="orderlist" border style="width: 100%">
        <el-table-column prop="orderSn" label="订单号" width="180">
        </el-table-column>
        <el-table-column prop="orderState" label="订单状态" width="180">
          <template slot-scope="scoped">
            <div>
              <el-tag v-if="scoped.row.orderState == 0" type="warning"
                >等待添加设备</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 1"
                >部分设备添加</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 2" type="success"
                >等待发货</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 3" type="success"
                >部分发货</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 4" type="success"
                >运输中</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 5" type="success"
                >部分签收</el-tag
              >
              <el-tag v-else-if="scoped.row.orderState == 6" type="success"
                >订单完成</el-tag
              >

              <!-- int	订单状态,0=等待添加设备,1=部分设备添加,2=等待发货,3=部分发货,4=运输中,5=部分签收,6=订单完成 -->
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="actualPayMoney" label="实际支付金额" width="120">
        </el-table-column>
        <el-table-column prop="needPayMoney" label="下单支付金额" width="120">
        </el-table-column>
        <el-table-column prop="contactAddress" label="配送地址" width="220">
        </el-table-column>
        <el-table-column prop="contactPhone" label="联系人电话" width="120">
        </el-table-column>
        <el-table-column prop="contactName" label="	联系人姓名" width="120">
        </el-table-column>
        <el-table-column prop="isSign" label="是否签收">
          <template slot-scope="scope">
            <div>
              <el-tag v-if="scope.row.isSign == 1" type="success">签收</el-tag>
              <el-tag v-else-if="scope.row.isSign == 2" type="danger"
                >未签收</el-tag
              >
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="备注" width="220">
        </el-table-column>
        <el-table-column prop="add_time" label="添加时间" width="220">
        </el-table-column>
        <el-table-column prop="update_time" label="更新数据时间" width="220">
        </el-table-column>
        <el-table-column prop="end_time" label="订单结束时间" width="220">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="300">
          <template slot-scope="scoped">
            <el-button
              type="success"
              size="mini"
              @click="handlerEditOrder(scoped.row.orderSn)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              size="mini"
              @click="handlerDelOrder(scoped.row.orderSn)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[10, 20, 30, 40, 50]"
          :page-size="orderSerach.pageSize"
          :current-page="orderSerach.pageNumber"
          layout="total, sizes, prev, pager, next, jumper"
          :total="count"
          class="management_page"
        ></el-pagination>
      </div>

      <!-- 添加订单信息 -->
      <el-dialog
        :title="orderName"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
        v-if="dialogVisible"
      >
        <el-form
          ref="orderFormRef"
          :model="orderForm"
          :rules="rules"
          label-width="120px"
        >
          <el-form-item label="联系人姓名" prop="contactName">
            <el-input
              v-model="orderForm.contactName"
              placeholder="请输入联系人姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="联系人电话" prop="contactPhone">
            <el-input
              v-model="orderForm.contactPhone"
              placeholder="请输入联系人电话"
            ></el-input>
          </el-form-item>
          <el-form-item label="配送地址" prop="contactAddress">
            <el-input
              v-model="orderForm.contactAddress"
              placeholder="请输入配送地址"
            ></el-input>
          </el-form-item>
          <el-form-item label="	实际支付金额" prop="actualPayMoney">
            <el-input
              v-model="orderForm.actualPayMoney"
              placeholder="请输入实际支付金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="	下单支付金额" prop="needPayMoney">
            <el-input
              v-model="orderForm.needPayMoney"
              placeholder="请输入下单支付金额"
            ></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="orderForm.remark"
              placeholder="请输入备注"
            ></el-input>
          </el-form-item>

          <div
            v-for="(item, index) in addForm.dynamicItem"
            :key="index"
            class="orderlistForm"
          >
            <el-form-item
              label="设备类型"
              :prop="'dynamicItem.' + index + '.deviceType'"
              :rules="{
                required: true,
                message: '设备名称不能为空',
                trigger: 'blur'
              }"
              class="orderlistForm_one"
            >
              <el-select
                v-model="item.deviceType"
                placeholder="设备类型"
                @change="handlergetModel"
              >
                <el-option
                  v-for="subitem in typelist"
                  :label="subitem.name"
                  :value="subitem.id"
                  :key="subitem.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="设备型号"
              :prop="'dynamicItem.' + index + '.deviceModel'"
              :rules="{
                required: true,
                message: '设备名称不能为空',
                trigger: 'blur'
              }"
              class="orderlistForm_two"
            >
              <el-select v-model="item.deviceModel" placeholder="设备型号">
                <el-option
                  v-for="sub1item in modellist"
                  :label="sub1item.name"
                  :value="sub1item.id"
                  :key="sub1item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              :prop="'dynamicItem.' + index + '.goodsNumber'"
              :rules="{
                required: true,
                message: '设备名称不能为空',
                trigger: 'blur'
              }"
              class="orderlistForm_three"
            >
              <el-input-number
                size="small"
                v-model="item.goodsNumber"
              ></el-input-number>
              <i class="el-icon-delete" @click="deleteItem(item, index)"></i>
              <el-button type="success" size="small" @click="handlerAdd"
                >添加设备</el-button
              >
            </el-form-item>
          </div>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="handlersureSubmit">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import {
  getOrderList,
  insertOrder,
  deleteOrder,
  typeGetModel,
  orderSnGetDetail,
  devicTypeList,
  devicModelList,
  updateOrder
} from "../../utils/api";

import { HidePhone } from "../../utils/common"; //A.引用其中某几个方法

export default {
  inject: ["reload"],
  data() {
    return {
      addForm: {
        deviceType: "",
        deviceModel: "",
        goodsNumber: "",
        dynamicItem: [
          {
            deviceType: "",
            deviceModel: "",
            goodsNumber: ""
          }
        ]
      },
      // 订单列表
      orderlist: [],
      //   搜索栏
      orderSerach: {
        orderSn: "",
        contactPhone: "",
        pageNumber: 1,
        pageSize: 10,
        orderType: 1
      },
      // 订单总条数
      count: 0,
      //判断是添加还是修改
      orderName: "新增订单",
      // 订单信息的弹框
      dialogVisible: false,
      // 订单表单信息
      orderForm: {
        actualPayMoney: "",
        needPayMoney: "",
        contactAddress: "",
        contactPhone: "",
        contactName: "",
        remark: "",
        goodsList: []
      },
      // 订单校验
      rules: {
        actualPayMoney: [
          { required: true, message: "请输入实际支付金额", trigger: "change" }
        ],
        needPayMoney: [
          { required: true, message: "请输入下单支付金额", trigger: "change" }
        ],
        contactAddress: [
          { required: true, message: "请输入配送地址", trigger: "change" }
        ],
        contactPhone: [
          { required: true, message: "请输入联系人电话", trigger: "change" }
        ],
        contactName: [
          { required: true, message: "请输入联系人姓名", trigger: "change" }
        ],
        remark: [{ required: true, message: "请输入备注", trigger: "change" }]
      },
      num3: 1,
      // 类型数据
      typelist: [],
      // 型号数据
      modellist: []
    };
  },
  created() {
    this.getOrder();
  },
  methods: {
    // 获取订单信息
    getOrder() {
      this.postRequest(getOrderList, this.orderSerach).then(res => {
        if (res.code == 200) {
          this.orderlist = res.data.list;
          this.count = res.data.count;
        }
      });
    },
    // 获取设备类型
    getdevice() {
      const params = {
        companyId: 20
      };
      this.postRequest(devicTypeList, params).then(res => {
        if (res.code == 200) {
          this.typelist = res.data;
        }
      });
    },
    //获取设备型号
    getdeviceMOdel() {
      const params = {
        companyId: 20
      };
      this.postRequest(devicModelList, params).then(res => {
        if (res.code == 200) {
          this.modellist = res.data;
        }
      });
    },

    // 获取设备型号 根据设备类型
    handlergetModel(val) {
      const params = {
        typeId: val
      };
      this.postRequest(typeGetModel, params).then(res => {
        if (res.code == 200) {
          this.modellist = res.data;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.orderSerach.pageSize = val;
      this.getOrder();
    },
    handleCurrentChange(val) {
      this.orderSerach.pageNumber = val;
      this.getOrder();
    },
    handleClose() {
      this.orderForm.actualPayMoney = "";
      this.orderForm.needPayMoney = "";
      this.orderForm.contactAddress = "";
      this.orderForm.contactPhone = "";
      this.orderForm.contactName = "";
      this.orderForm.remark = "";
      this.orderForm.goodsList = "";
      this.dialogVisible = false;
      this.reload();
    },
    // 点击添加现实订单弹框
    showOrder() {
      this.dialogVisible = !this.dialogVisible;
      this.getdevice();
    },
    // 动态添加设备
    handlerAdd() {
      this.addForm.dynamicItem.push({
        deviceType: "",
        deviceModel: "",
        goodsNumber: ""
      });
    },
    // 删除设备
    deleteItem(item, index) {
      this.index = this.addForm.dynamicItem.indexOf(item);
      if (index !== -1) {
        this.addForm.dynamicItem.splice(index, 1);
      }
    },
    // 点击确定添加订单
    handlersureSubmit() {
      this.orderForm.goodsList = JSON.stringify(this.addForm.dynamicItem);
      if (this.orderName == "新增订单") {
        this.postRequest(insertOrder, this.orderForm).then(res => {
          if (res.code == 200) {
            this.$message.success("新增订单成功");
            this.getOrder();
            this.dialogVisible = false;
            this.$refs["orderFormRef"].resetFields();
          }
        });
      } else if (this.orderName == "编辑订单") {
        this.postRequest(updateOrder, this.orderForm).then(res => {
          if (res.code == 200) {
            this.$message.success("修改订单成功");
            this.getOrder();
            this.$refs["orderFormRef"].resetFields();
            this.dialogVisible = false;
          }
        });
      }
    },
    // 点击编辑显示设备
    handlerEditOrder(orderSn) {
      this.orderName = "编辑订单";
      this.getdevice();
      this.getdeviceMOdel();
      this.postRequest(orderSnGetDetail, { orderSn: orderSn }).then(res => {
        if (res.code == 200) {
          this.orderForm = res.data;
          this.addForm.dynamicItem = res.data.goods;
          this.dialogVisible = true;
        }
      });
    },
    // 点击删除订单
    handlerDelOrder(orderSn) {
      this.$confirm("此操作将永久删除该订单, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.postRequest(deleteOrder, { orderSn: orderSn }).then(res => {
            if (res.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              this.getOrder();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //点击查看设备
    handlershowDevice(orderSn) {
      window.sessionStorage.setItem("orderSn", orderSn);
      this.$router.push("/orderdevice");
    }
  }
};
</script>

<style>
.orderlistForm {
  display: flex;
  flex-direction: row;
}

.orderlistForm .orderlistForm_one {
  width: 358rpx;
}
.orderlistForm .orderlistForm_two {
  width: 358rpx;
}
.orderlistForm .orderlistForm_three {
  width: 450rpx;
}
</style>
